package com.example.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.demo.dao.UserDao;
import com.example.demo.domain.Users;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.List;

@RestController
@RequestMapping("users")
public class UserController{
    @Autowired
    private UserDao user;

    @GetMapping
//    @CrossOrigin
    public List<Users> getall(){
        System.out.println(user.selectList(null));
        return user.selectList(null);
    }

    @GetMapping("{id}")
    public Users getbyid(@PathVariable Integer id){
        return user.selectById(id);
    }
    @PostMapping("{pathname}")
    public List<Users> getbyname(@PathVariable String pathname){
        QueryWrapper qw =new QueryWrapper();
        qw.like("username",pathname);
        return user.selectList(qw);
    }

   @PostMapping
    public Boolean saveUser(@RequestBody Users u){
         user.insert(u);
        return true;

    }

    @DeleteMapping("{id}")
    public Boolean deletebyid(@PathVariable Integer id){
        user.deleteById(id);
        return true;
    }
    @PutMapping
    public Boolean update(@RequestBody Users u){
        u.setDeleted(0);
        u.setTenant_id(0);
        u.setUpdate_time( "2008-07-10 19:20:00" );
        u.setCreate_time("2008-07-10 19:20:00");
        user.updateById(u);
        return true;
    }
}
